<?php
namespace Admin\Controller;
use Think\Controller;
class PhotoController extends CommonController {
	public function _initialize(){
		parent::_initialize();
	}
    public function add(){
		$this->display();
    }
	public function addArticle(){
		$rules = array(
			array('title','require','相册名必须！'), // 用户名必须
			array('picture','require','封面必须！'), // 用户名必须
		);
		$addArticle = M ("Photo");
		$data = $addArticle->validate($rules)->create();
		if(!$data){
			$this->error($addArticle->getError());
		}else{
			$data['addtime']=time();
			$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize = 3145728 ;// 设置附件上传大小
			$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$path=realpath(__ROOT__);
			$upload->rootPath =  $path.'/Public/uploadfiles/'; // 设置附件上传根目录
			$upload->savePath =  'uploadphotos/'; // 设置附件上传（子）目录
			// 上传文件 
			$info   =   $upload->upload();				
			if(!$info) {// 上传错误提示错误信息
				$this->error($upload->getError());
			}else{// 上传成功
				$data['picture']="/Public/uploadfiles/{$info['picture']['savepath']}{$info['picture']['savename']}";     
				$addArticle->add($data);
				$this->success("成功！");
			}
		}				
	}
	 public function photo_alist(){
		//相册分页
		$id = I ('get.id');
		$Photo = M('Photo'); // 实例化User对象
		$count      = $Photo->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $Photo->order('id asc')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		//删除单个相册操作
		$act = I ('get.act');
		if($act == 'del'){
			$res = $Photo->where("id={$id}")->delete();
			$image = M('Images');
			$image_res = $image->where("photo_id={$id}")->delete();
			if($image_res != false){
				if($res != false){
					$this->success("删除成功");
				}else{
					$this->error("请重新删除！");
					}
			}
			else{
				$this->error($Photo->getError());
			}
			return;		
		}
		$this->display(); // 输出模板
		
    }
    public function uploadphoto(){
		$id = I ('get.id');
		$this->assign('id',$id);
		$this->display(); // 输出模板	
    }
    public function imgphotos(){
		$shop_id = I('get.shop_id');	
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   = 2 * 1024 *1024 ;// 设置附件上传大小
		$upload->exts      = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$path = realpath(__ROOT__);
		$upload->rootPath  = $path.'/Public/uploadfiles/'; // 设置附件上传根目录
		$upload->savePath  = 'uploadphotos/'; // 设置附件上传（子）目录
		// 上传文件 
		$info   =   $upload->upload();
		if(!$info) {// 上传错误提示错误信息
			$this->error($upload->getError());
		}else{// 上传成功
			$img_src = "/Public/uploadfiles/{$info['_img']['savepath']}{$info['_img']['savename']}";
			
			$thumbpath = $path."/Public/uploadfiles/{$info['_img']['savepath']}";
			$image = new \Think\Image(); 
			$image->open("{$path}{$img_src}");
			$width = $image->width();//返回图片宽度
			$height = $image->height();///返回图片高度
			// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
			$image->thumb($width, $height)->text('ThinkPHP')->save($thumbpath.$info['_img']['savename']);
			$image->thumb(220, 220,6)->save($thumbpath."min_".$info['_img']['savename']);
			
			
			$data['shop_id']	= $shop_id;
			$data['picture'] 	= $img_src;
			$data['addtime']	= time();
			
			$Shop = M('Shop');
			
			$Shop->add($data);
			//$this->success($this->display("uploadphoto"));
			echo "图片上传成功!----{$data['picture']}";
		}		
	
		}
		
		public function photosee(){
			$id = I ('get.id');
			$image = M ('Images');
			$count      = $image->where("photo_id={$id}")->count();// 查询满足要求的总记录数
			$Page       = new \Think\Page($count,6);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			
			$res = $image->order('id desc')->where("photo_id={$id}")->limit($Page->firstRow.','.$Page->listRows)->select();
			$show       = $Page->show();// 分页显示输出
			$this->assign('res',$res);// 赋值数据集
			$this->assign('page',$show);// 赋值分页输出
			$this->display(); // 输出模板
		
			}
		
		
		
		
		
		
		
		
		
}//
